package iut2;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.torque.TorqueException;
import org.apache.torque.util.Criteria;

import iut2.torque.generated.BaseEtudiantPeer;
import iut2.torque.generated.EtudiantPeer;
import iut2.torque.generated.Etudiant;
import iut2.torque.generated.Groupe;
import iut2.torque.generated.GroupePeer;
import iut2.torque.generated.NotePeer;


@SuppressWarnings("serial")
public class Controleur extends HttpServlet {
	
	private String urlHome;
	private String urlListerNotes;
	private String urlListerAbs;
	private String urlEditNotes;
	private String urlEditAbs;
	private String urlGestionEtudiants;
	private String urlGestionGroupes;
	
	public List<Etudiant> Etu;
	public List<Groupe> Groups;
	
	// INIT
	public void init() {
		
		urlHome 			= getServletConfig().getInitParameter("home");
		urlListerNotes 		= getServletConfig().getInitParameter("listerNotes");
		urlListerAbs 		= getServletConfig().getInitParameter("listerAbsences");
		urlEditNotes 		= getServletConfig().getInitParameter("editerNotes");
		urlEditAbs 			= getServletConfig().getInitParameter("editerAbsences");
		urlGestionEtudiants = getServletConfig().getInitParameter("gestionEtudiants");
		urlGestionGroupes	= getServletConfig().getInitParameter("gestionGroupes");	
	}
	
	// POST
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		
		doGet(request, response);
	}
	
	// GET
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		
		String methode = request.getMethod().toLowerCase();
		
		String action = request.getPathInfo();
		if(action == null) action = "/home";
		
		String ifGroup = request.getParameter("group");
		String ifId = request.getParameter("id");
		
		EtudiantPeer EP = new EtudiantPeer();
		NotePeer NP = new NotePeer();
		GroupePeer grp = new GroupePeer();

		if (request.getParameter("delEtu") != null)
		{
			EP.SupprimerEtdiant(Integer.parseInt(request.getParameter("delEtu")));			
		}
		if (request.getParameter("majEtu") != null)
		{
			EP.SupprimerEtdiant(Integer.parseInt(request.getParameter("majEtu")));			
		}
		if (request.getParameter("majGrp") != null)
		{
			EP.SupprimerEtdiant(Integer.parseInt(request.getParameter("majGrp")));			
		}
		if (request.getParameter("delNote") != null)
		{
			 NP.SupprimerNote(Integer.parseInt(request.getParameter("delNote")));			
		}
		if (request.getParameter("addGrp") != null)
		{
			grp.ajouterGroupe(request.getParameter("addGrp"));			
		}
		if (request.getParameter("addEtu") != null)
		{
			EP.AjouterEtdiant(request.getParameter("addEtu"), request.getParameter("prenom"), request.getParameter("groupe"));			
		}	
		if (request.getParameter("addNoteEtu") != null)
		{
			NP.AjouteNote(request.getParameter("addNoteEtu"), request.getParameter("note"));			
		}
		if (request.getParameter("delGrp") != null)
		{
			grp.SupprimerGroupe(request.getParameter("delGrp"));	
		}
		
		
		if (request.getParameter("maj") != null)
		{
			String [] ids = request.getParameterValues("maj");
			
			int size = ids.length;
			
			if (request.getParameter("abs") != null)
			{
				String [] abs = request.getParameterValues("abs");
			
				for(int i = 0; i < size; i++)
				{
					EP.MajAbs(Integer.parseInt(ids[i]), Integer.parseInt(abs[i]));
				}
			}
			
			if (request.getParameter("notes") != null)
			{
				String [] notes = request.getParameterValues("notes");
				for(int i = 0; i < size; i++)
				{
					notes[i] = notes[i].replace(",", ".");
					NP.MajNote(Integer.parseInt(ids[i]), Double.parseDouble(notes[i]));
				}
			}
			
			if (request.getParameter("groups") != null)
			{
				String [] groups = request.getParameterValues("groups");
				for(int i = 0; i < size; i++)
				{
					grp.MajGroups(Integer.parseInt(ids[i]), groups[i]);
				}
			}
			
			if (
					request.getParameter("name") != null &&
					request.getParameter("firstName") != null &&
					request.getParameter("groupe") != null
			)
			{
				String [] name = request.getParameterValues("name");
				String [] fname = request.getParameterValues("firstName");
				String [] groupe = request.getParameterValues("groupe");
				for(int i = 0; i < size; i++)
				{
					EP.MajEtus(Integer.parseInt(ids[i]), name[i], fname[i], Integer.valueOf(groupe[i]));
				}
			}
		}
				
		//if(ifGroup != null) Etu = GestionFactory.getEtudiantsParGroupe(ifGroup);
		if(ifGroup != null)
			try {
				Etu = EP.getEtudiantParGroupe(Integer.parseInt(ifGroup));
			} catch (NumberFormatException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (TorqueException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		else if(ifId != null) { 
			Etu = EP.getEtudiantById(Integer.parseInt(ifId));
		} 
		else {
			try {
				Etu = EP.getEtudiants();
			} catch (TorqueException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}			
		
		request.setAttribute("list", Etu);
		request.setAttribute("groups_list", grp.getGroupes());
		
		if (action.equals("/listerNotes")) {			
			loadJSP(urlListerNotes, request, response);
		
		} else if (action.equals("/listerAbsences")) {			
			loadJSP(urlListerAbs, request, response);
		
		} else if (action.equals("/editerNotes")) {
			loadJSP(urlEditNotes, request, response);
		
		} else if (action.equals("/editerAbsences")) {
			loadJSP(urlEditAbs, request, response);
		
		} else if (action.equals("/gestionGroupes")) {
			loadJSP(urlGestionGroupes, request, response);
		
		}else if (action.equals("/gestionEtudiants")) {
			loadJSP(urlGestionEtudiants, request, response);
		
		}else {
			loadJSP(urlHome, request, response);
		}
		
	}
	
	public void loadJSP(String url, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		ServletContext sc = getServletContext();
		RequestDispatcher rd = sc.getRequestDispatcher(url);
		rd.forward(request, response);
	}

}
